package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseStoredValueRulesBo;
import com.rla.domain.vo.BaseStoredValueRulesVo;
import com.rla.domain.BaseStoredValueRules;
import com.rla.mapper.BaseStoredValueRulesMapper;
import com.rla.service.IBaseStoredValueRulesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 储值规则Service业务层处理
 *
 * @author rla
 * @date 2025-09-09
 */
@RequiredArgsConstructor
@Service
public class BaseStoredValueRulesServiceImpl implements IBaseStoredValueRulesService {

    private final BaseStoredValueRulesMapper baseMapper;

    /**
     * 查询储值规则
     */
    @Override
    public BaseStoredValueRulesVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询储值规则列表
     */
    @Override
    public TableDataInfo<BaseStoredValueRulesVo> queryPageList(BaseStoredValueRulesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseStoredValueRules> lqw = buildQueryWrapper(bo);
        Page<BaseStoredValueRulesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询储值规则列表
     */
    @Override
    public List<BaseStoredValueRulesVo> queryList(BaseStoredValueRulesBo bo) {
        LambdaQueryWrapper<BaseStoredValueRules> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseStoredValueRules> buildQueryWrapper(BaseStoredValueRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseStoredValueRules> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRecharge()), BaseStoredValueRules::getRecharge, bo.getRecharge());
        lqw.eq(StringUtils.isNotBlank(bo.getAfterSales()), BaseStoredValueRules::getAfterSales, bo.getAfterSales());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomAmount()), BaseStoredValueRules::getCustomAmount, bo.getCustomAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getGiftsAreAllowed()), BaseStoredValueRules::getGiftsAreAllowed, bo.getGiftsAreAllowed());
        lqw.eq(StringUtils.isNotBlank(bo.getGiftRulesIntegral()), BaseStoredValueRules::getGiftRulesIntegral, bo.getGiftRulesIntegral());
        lqw.eq(StringUtils.isNotBlank(bo.getGiftRulesAmount()), BaseStoredValueRules::getGiftRulesAmount, bo.getGiftRulesAmount());
        lqw.eq(bo.getPointsRecharged() != null, BaseStoredValueRules::getPointsRecharged, bo.getPointsRecharged());
        lqw.eq(bo.getPointsRatio() != null, BaseStoredValueRules::getPointsRatio, bo.getPointsRatio());
        lqw.eq(bo.getAmountValue() != null, BaseStoredValueRules::getAmountValue, bo.getAmountValue());
        lqw.eq(bo.getAmountRatio() != null, BaseStoredValueRules::getAmountRatio, bo.getAmountRatio());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcCreator()), BaseStoredValueRules::getFgcCreator, bo.getFgcCreator());
        lqw.eq(bo.getFgcCreatedate() != null, BaseStoredValueRules::getFgcCreatedate, bo.getFgcCreatedate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcLastmodifier()), BaseStoredValueRules::getFgcLastmodifier, bo.getFgcLastmodifier());
        lqw.eq(bo.getFgcLastmodifydate() != null, BaseStoredValueRules::getFgcLastmodifydate, bo.getFgcLastmodifydate());
        lqw.eq(bo.getFgcRowversion() != null, BaseStoredValueRules::getFgcRowversion, bo.getFgcRowversion());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcUpdatehelp()), BaseStoredValueRules::getFgcUpdatehelp, bo.getFgcUpdatehelp());
        return lqw;
    }

    /**
     * 新增储值规则
     */
    @Override
    public Boolean insertByBo(BaseStoredValueRulesBo bo) {
        BaseStoredValueRules add = BeanUtil.toBean(bo, BaseStoredValueRules.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改储值规则
     */
    @Override
    public Boolean updateByBo(BaseStoredValueRulesBo bo) {
        BaseStoredValueRules update = BeanUtil.toBean(bo, BaseStoredValueRules.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseStoredValueRules entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除储值规则
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
